package com.jinlejia.erp.widget.prospect;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jinlejia.erp.R;

/**
 * @author 王进 on 2018/12/25.
 */

public class ProspectRadioImageButton extends RelativeLayout{

    public ProspectRadioImageButton(Context context) {
        this(context,null);
    }

    public ProspectRadioImageButton(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    private ImageView imageView;
    private TextView textView;
    private int radioBgNormal = -1;
    private int radioBgChecked = -1;
    private int radioSrc = -1;
    private CharSequence radioText = null;
    private int radioTextColor = Color.BLACK;
    private int radioTextSize = 23;
    private boolean mCheck = false;
    private RelativeLayout rootLayout;
    public ProspectRadioImageButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view = LayoutInflater.from(context).inflate(R.layout.widget_prospect_house_radio, this, true);
        this.imageView = view.findViewById(R.id.imageView);
        this.textView = view.findViewById(R.id.textView);
        this.rootLayout = view.findViewById(R.id.rootLayout);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ProspectRadioImageButton);
        radioSrc = ta.getResourceId(R.styleable.ProspectRadioImageButton_radio_src,radioSrc); //图片源
        radioBgNormal =  ta.getResourceId(R.styleable.ProspectRadioImageButton_radio_normal,radioBgNormal); //背景普通
        radioBgChecked =  ta.getResourceId(R.styleable.ProspectRadioImageButton_radio_checked,radioBgChecked); //背景选中
        radioText = ta.getText(R.styleable.ProspectRadioImageButton_radio_text); //文本
        radioTextColor = ta.getColor(R.styleable.ProspectRadioImageButton_radio_textColor,radioTextColor); //文本颜色
        radioTextSize = ta.getDimensionPixelSize(R.styleable.ProspectRadioImageButton_radio_textSize,radioTextSize); //文本大小
        mCheck = ta.getBoolean(R.styleable.ProspectRadioImageButton_radioChecked, mCheck);
        ta.recycle();

        initView();
    }
    private ProspectRadioImageButtonListener listener;

    public void setListener(ProspectRadioImageButtonListener listener) {
        this.listener = listener;
    }

    private void initView() {
        setChecked(mCheck);
        textView.setText(radioText);
        textView.setTextSize(radioTextSize);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,radioTextSize);
        textView.setTextColor(radioTextColor);

        imageView.setImageResource(radioSrc);
    }

    public void setChecked(boolean checked){
        if (checked){
            if (listener!=null){
                boolean b = listener.onChecked(this);
                if (!b){
                    rootLayout.setBackgroundDrawable(getResources().getDrawable(radioBgChecked));
                    this.mCheck = true;
                }
            }
        }else {
            rootLayout.setBackgroundDrawable(getResources().getDrawable(radioBgNormal));
            this.mCheck = false;
        }

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = getMeasuredWidth();

        super.onMeasure(widthMeasureSpec, widthMeasureSpec);


    }

    public boolean isChecked(){
        return this.mCheck;
    }

    public interface ProspectRadioImageButtonListener{
        /**
         *
         * @return false 则展示选中的展示效果 true 消耗掉当前事件
         */
        boolean onChecked(ProspectRadioImageButton button);
    }
}
